﻿using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.Collections.Extensions;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using Microsoft.EntityFrameworkCore;
using Mt.Site.Application.DataLogs.BackEnd.Dto;
using Mt.Site.Application.Dto;
using Mt.Site.Core.DataLogs;
using Mt.Site.Core.DataLogs.Permissions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Mt.Site.Application.DataLogs.BackEnd
{
    public class DataLogsAppService : ApplicationService, IDataLogsAppService
    {
        public readonly IRepository<DataLog, Guid> _dataLogRepository;

        public DataLogsAppService(IRepository<DataLog, Guid> dataLogRepository)
        {
            _dataLogRepository = dataLogRepository;
        }

        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize(DataLogsPermissions.Pages_Manage_DataLogs_Query)]
        public async Task<PagedResultDto<PageDataLogsInput>> GetPagedDataLogsInfo(PageDataLogsInput input)
        {
            DateTime? startTime = input.StartTime;
            DateTime? endTime = input.EndTime;

            if (endTime == null || startTime == null)
            {
                startTime = null;
                endTime = null;
            }
            if (startTime > endTime)
            {
                startTime = null;
                endTime = null;
            }
            var query = _dataLogRepository.GetAll()
                       .WhereIf(!input.Filter.IsNullOrEmpty(), t => t.DataTitle.Contains(input.Filter))
                       .WhereIf(input.StartTime != null && input.EndTime != null, item => item.CreationTime >= startTime && item.CreationTime <= endTime); ;
            query = query.OrderByDescending(t => t.CreationTime);
            var DataLogssCount = query.Count();
            var DataLogssList = await query.PageBy(input).ToListAsync();
            
            return new PagedResultDto<PageDataLogsInput>(DataLogssCount, ObjectMapper.Map<List<PageDataLogsInput>>(DataLogssList));

        }

        /// <summary>
        /// 根据id获取全部会员信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>

        [AbpAuthorize(DataLogsPermissions.Pages_Manage_DataLogs_Query)]
        public async Task<DataLog> GetDataLogsByIdAsync(EntityDto<Guid> input)
        {
            var query = await _dataLogRepository.GetAsync(input.Id);

            var dto = ObjectMapper.Map<DataLog>(query);

            return dto;
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize(DataLogsPermissions.Pages_Manage_DataLogs_Delete)]
        public async Task DeleteListAsync(EntityIdListDto input)
        {
            foreach (var id in input.List)
            {
                await _dataLogRepository.DeleteAsync(id);
            }
        }
    }
}
